package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.result.R;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /*
    * 员工登陆功能
    * */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        R login = employeeService.login(request,employee);
        return login;
    }

    /*
    * 员工退出功能
    * */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");

        return R.success("退出成功");
    }

    /*
    * 新增员工功能
    * */
    @PostMapping
    public R<String> addEmployee(HttpServletRequest request,@RequestBody Employee employee) {
        //默认密码并md5加密  123456
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //自动填充时间
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());
        //获得当前登录用户的id
       // Long empId = (Long) request.getSession().getAttribute("employee");
        //employee.setCreateUser(empId);
        //employee.setUpdateUser(empId);
        //执行添加方法
        employeeService.save(employee);
        return R.success("添加成功");
    }


    /*
    * 员工姓名分页查询
    * */

    @GetMapping("/page")
    public R<Page> selectPage(int page, int pageSize, String name) {
        log.info("page = {},pageSize = {},name = {}" ,page,pageSize,name);
        //分页器
        Page page1 = new Page(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //实现名字分页的查询
        lqw.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        //排序
        lqw.orderByDesc(Employee::getUpdateTime);

        //查询
        employeeService.page(page1, lqw);

        return R.success(page1);
    }


    /*
     * 根据Id修改状态，修改员工信息
     * */
    @PutMapping
    public R<String> updateStatus(HttpServletRequest request,@RequestBody Employee employee) {
        /*Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(empId);*/
        employeeService.updateById(employee);
        return R.success("状态修改成功！");
    }

    /*
     * 根据id查询用户
     * */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);
        if (null != employee) {
            return R.success(employee);
        }
        return R.error("用户不存在！");
    }

}
